package com.rules.admin.controller;

import com.rules.admin.utils.ResultUtils;
import com.rules.admin.entity.UnitCaseMock;
import com.rules.admin.service.UnitCaseMockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping({"/unitcases/"})
public class UnitCaseMockController {
   @Autowired
   private UnitCaseMockService unitCaseMockService;

   @RequestMapping(
      value = {"/mocks"},
      method = {RequestMethod.POST}
   )
   @ResponseBody
   public Map<String, Object> createMock(@RequestBody UnitCaseMock unitCaseMock) {
      this.unitCaseMockService.save(unitCaseMock);
      return ResultUtils.getSuccessResultData(unitCaseMock);
   }

   @RequestMapping(
      value = {"/mocks/{id}"},
      method = {RequestMethod.DELETE}
   )
   @ResponseBody
   public Map<String, Object> deleteMock(@PathVariable("id") Long id) {
      boolean result = this.unitCaseMockService.removeById(id);
      return result ? ResultUtils.getSuccessResultData() : ResultUtils.getFaildResultData(new String[]{"删除失败"});
   }

   @RequestMapping(
      value = {"/mocks/{id}"},
      method = {RequestMethod.PUT}
   )
   @ResponseBody
   public Map<String, Object> updateMock(@RequestBody UnitCaseMock expectMock) {
      this.unitCaseMockService.save(expectMock);
      return ResultUtils.getSuccessResultData(expectMock);
   }

   @RequestMapping(
      value = {"/mocks"},
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public List<UnitCaseMock> findMock(Long unitCaseId) {
      return this.unitCaseMockService.getAllMocksByCaseId(unitCaseId);
   }
}
